package liumengbaio.lolzhangyoubao.Activity.Fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.InFo.BroAndPaInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.FreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.SkillsInfo;
import liumengbaio.lolzhangyoubao.Activity.utils.BroAndPaUtils;
import liumengbaio.lolzhangyoubao.Activity.utils.SkillsUtils;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/8/23.
 */
public class FragmentHeroIntroduce extends Fragment implements View.OnClickListener {
    private TextView base_blood, base_magic, base_physical, base_difficulty, moving_speed, range, attack, attack_speed, armor, blood,
            mana, magic_resistance, blood_recovery, magic_recovery, usetip, killtip, bg;
    private ProgressBar physicalProgressBar, magicProgressBar, bloodProgressBar, difficultyProgressBar,levelProgressBar;
    private List<BroAndPaInfo> mList;
    private List<BroAndPaInfo> lists;
    private List<BroAndPaInfo> brolists;
    private List<SkillsInfo> alist;
    private SkillsInfo plists;
    private SkillsInfo qlists;
    private SkillsInfo wlists;
    private SkillsInfo elists;
    private SkillsInfo rlists;
    private ImageView iv_paone, iv_patwo, iv_broone, iv_brotwo, skill_passive, skill_q, skill_w, skill_e, skill_r;
    private TextView tv_paone, tv_patwo, tv_broone, tv_brotwo, skill_name, skill_introduce, skill_colling, skill_expend,tv_levelnumber;
    private RelativeLayout rl_skill_passive,rl_skill_q,rl_skill_w,rl_skill_e,rl_skill_r;
    private Button btn_seeVideo,btn_reduce,btn_add;
    private String id;
    String firstStr;
    String secondStr;
    String thirdStr;
    String fourthStr;
    String fifthStr;
    String sixthStr,sevenStr;

    int a =50; Double b =0.66; Double c= 6.8;int d =37; Double e =0.8; Double f =3.5;


    public static FragmentHeroIntroduce newInstants(FreeHeroInfo info) {
        FragmentHeroIntroduce fragmentHeroIntroduce = new FragmentHeroIntroduce();
        Bundle bundle = new Bundle();
        bundle.putSerializable("introduceinfo", info);
        fragmentHeroIntroduce.setArguments(bundle);
        return fragmentHeroIntroduce;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_herointroduce, null);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        initData();
        initEvent();
    }


    private void initView(View view) {
        rl_skill_passive= (RelativeLayout) view.findViewById(R.id.rl_skill_passive);
        rl_skill_q= (RelativeLayout) view.findViewById(R.id.rl_skill_q);
        rl_skill_w= (RelativeLayout) view.findViewById(R.id.rl_skill_w);
        rl_skill_e= (RelativeLayout) view.findViewById(R.id.rl_skill_e);
        rl_skill_r= (RelativeLayout) view.findViewById(R.id.rl_skill_r);
        tv_levelnumber= (TextView) view.findViewById(R.id.tv_levelnumber);
        iv_paone = (ImageView) view.findViewById(R.id.iv_paone);
        iv_patwo = (ImageView) view.findViewById(R.id.iv_patwo);
        iv_broone = (ImageView) view.findViewById(R.id.iv_broone);
        iv_brotwo = (ImageView) view.findViewById(R.id.iv_brotwo);
        btn_seeVideo= (Button) view.findViewById(R.id.btn_seeVideo);
        btn_reduce= (Button) view.findViewById(R.id.btn_reduce);
        btn_add= (Button) view.findViewById(R.id.btn_add);
        skill_passive = (ImageView) view.findViewById(R.id.skill_passive);
        skill_q = (ImageView) view.findViewById(R.id.skill_q);
        skill_w = (ImageView) view.findViewById(R.id.skill_w);
        skill_e = (ImageView) view.findViewById(R.id.skill_e);
        skill_r = (ImageView) view.findViewById(R.id.skill_r);
        skill_passive.setOnClickListener(this);
        skill_q.setOnClickListener(this);
        skill_w.setOnClickListener(this);
        skill_e.setOnClickListener(this);
        skill_r.setOnClickListener(this);
        tv_paone = (TextView) view.findViewById(R.id.tv_paone);
        tv_patwo = (TextView) view.findViewById(R.id.tv_patwo);
        tv_broone = (TextView) view.findViewById(R.id.tv_broone);
        tv_brotwo = (TextView) view.findViewById(R.id.tv_brotwo);
        skill_name = (TextView) view.findViewById(R.id.skill_name);
        skill_introduce = (TextView) view.findViewById(R.id.skill_introduce);
        skill_colling = (TextView) view.findViewById(R.id.skill_colling);
        skill_expend = (TextView) view.findViewById(R.id.skill_expend);
        levelProgressBar= (ProgressBar) view.findViewById(R.id.level_progressBar);
        physicalProgressBar = (ProgressBar) view.findViewById(R.id.physics_progressBar);
        magicProgressBar = (ProgressBar) view.findViewById(R.id.magic_progressBar);
        bloodProgressBar = (ProgressBar) view.findViewById(R.id.blood_progressBar);
        difficultyProgressBar = (ProgressBar) view.findViewById(R.id.difficulty_progressBar);
        moving_speed = (TextView) view.findViewById(R.id.moving_speed);
        range = (TextView) view.findViewById(R.id.range);
        attack = (TextView) view.findViewById(R.id.attack);
        attack_speed = (TextView) view.findViewById(R.id.attack_speed);
        armor = (TextView) view.findViewById(R.id.armor);
        blood = (TextView) view.findViewById(R.id.blood);
        mana = (TextView) view.findViewById(R.id.mana);
        magic_resistance = (TextView) view.findViewById(R.id.magic_resistance);
        magic_recovery = (TextView) view.findViewById(R.id.magic_recovery);
        usetip = (TextView) view.findViewById(R.id.usetip);
        killtip = (TextView) view.findViewById(R.id.killtip);
        bg = (TextView) view.findViewById(R.id.bg);
        blood_recovery = (TextView) view.findViewById(R.id.blood_recovery);

    }

    private void initData() {
        FreeHeroInfo info = (FreeHeroInfo) getArguments().getSerializable("introduceinfo");
        id = info.getId();
        moving_speed.setText(info.getMoving_speed());
        range.setText(info.getRange());
        attack.setText(info.getAttack());
        attack_speed.setText(info.getAttack_speed());
        armor.setText(info.getArmor());
        blood.setText(info.getBlood());
        mana.setText(info.getMana());
        magic_resistance.setText(info.getMagic_resistance());
        magic_recovery.setText(info.getMagic_recovery());
        usetip.setText(info.getUsetip());
        killtip.setText(info.getKilltip());
        bg.setText(info.getBg());
        blood_recovery.setText(info.getBlood_recovery());
        physicalProgressBar.setMax(100);
        physicalProgressBar.setProgress(Integer.parseInt(info.getBase_physical()));
        magicProgressBar.setMax(100);
        magicProgressBar.setProgress(Integer.parseInt(info.getBase_magic()));
        bloodProgressBar.setMax(100);
        bloodProgressBar.setProgress(Integer.parseInt(info.getBase_blood()));
        difficultyProgressBar.setMax(100);
        difficultyProgressBar.setProgress(Integer.parseInt(info.getBase_difficulty()));
        levelProgressBar.setMax(18);
        levelProgressBar.setProgress(1);


        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (levelProgressBar.getProgress()<18){
                    String add = String.valueOf(levelProgressBar.getProgress()+1);
                    tv_levelnumber.setText(add);
                    levelProgressBar.setProgress(levelProgressBar.getProgress()+1);
                    String str1 = (String) blood.getText();
                    firstStr = str1.substring(0, 3);
                    String srt2 = (String) attack_speed.getText();
                    secondStr = srt2.substring(0,3);
                    String str3 = (String) blood_recovery.getText();
                    thirdStr = str3.substring(0,3);
                    String str4 = (String) mana.getText();
                    fourthStr = str4.substring(0,3);
                    String str5 = (String) armor.getText();
                    fifthStr = str5.substring(0, 2);
                    String str6 = (String) magic_recovery.getText();
                    sixthStr = str6.substring(0);
                    String str7 = (String) attack.getText();
                    sevenStr =str7.substring(0,2);
                    int a = Integer.parseInt(firstStr)+90;
                    blood.setText(String.valueOf(a));
                    double b = Double.parseDouble(secondStr)+1;
                    attack_speed.setText(String.valueOf(b));
                    double c = Double.parseDouble(thirdStr)+1;
                    blood_recovery.setText(String.valueOf(c));
                    int d = Integer.parseInt(fourthStr)+37;
                    mana.setText(String.valueOf(d));
                    int e = Integer.parseInt(fifthStr)+4;
                    armor.setText(String.valueOf(e));

                    magic_recovery.setText("6");
                    int g = Integer.parseInt(sevenStr)+4;
                    attack.setText(String.valueOf(g));
                    magic_resistance.setText("30");

                }


            }
        });
        btn_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (levelProgressBar.getProgress()>1){
                    String reduce = String.valueOf(levelProgressBar.getProgress()-1);
                    tv_levelnumber.setText(reduce);
                    levelProgressBar.setProgress(levelProgressBar.getProgress()-1);

                    String str1 = (String) blood.getText();

                    String srt2 = (String) attack_speed.getText();

                    String str3 = (String) blood_recovery.getText();

                    String str4 = (String) mana.getText();

                    String str5 = (String) armor.getText();

                 String str6 = (String) magic_recovery.getText();

                    String str7 = (String) attack.getText();
                    int a = Integer.parseInt(str1)-9;
                    blood.setText(String.valueOf(a));
                    double b = Double.parseDouble(srt2)-1;
                    attack_speed.setText(String.valueOf(b));
                    double c = Double.parseDouble(str3)-1;
                    blood_recovery.setText(String.valueOf(c));
                    int d = Integer.parseInt(str4)-3;
                    mana.setText(String.valueOf(d));
                    int e = Integer.parseInt(str5)-4;
                    armor.setText(String.valueOf(e));
                    int g = Integer.parseInt(str7)-4;
                    attack.setText(String.valueOf(g));
                }



            }
        });


    }

    private void initEvent() {
        mList = BroAndPaUtils.getBroAndPaInfo(getActivity());
        lists = new ArrayList<>();
        brolists = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            BroAndPaInfo infos = mList.get(i);
            if (infos.getRole_id().equals(id) && infos.getRelation().equals("pa")) {
                lists.add(infos);
            } else if (infos.getRole_id().equals(id) && infos.getRelation().equals("bro")) {
                brolists.add(infos);
            }
        }
        Picasso.with(getActivity()).load(lists.get(0).getTarget_role_pic_url()).into(iv_paone);
        Picasso.with(getActivity()).load(lists.get(1).getTarget_role_pic_url()).into(iv_patwo);
        Picasso.with(getActivity()).load(brolists.get(0).getTarget_role_pic_url()).into(iv_broone);
        Picasso.with(getActivity()).load(brolists.get(1).getTarget_role_pic_url()).into(iv_brotwo);
        tv_paone.setText(lists.get(0).getDesc());
        tv_patwo.setText(lists.get(1).getDesc());
        tv_broone.setText(brolists.get(0).getDesc());
        tv_brotwo.setText(brolists.get(1).getDesc());
        alist = SkillsUtils.getSkillsInfo(getActivity());
        plists = new SkillsInfo();
        qlists = new SkillsInfo();
        wlists = new SkillsInfo();
        elists = new SkillsInfo();
        rlists = new SkillsInfo();
        for (int i = 0; i < alist.size(); i++) {
            SkillsInfo skillinfo = alist.get(i);
            if (skillinfo.getRole_id().equals(id) && skillinfo.getPassive().equals("1")) {
                plists=skillinfo;
            } else if (skillinfo.getRole_id().equals(id) && skillinfo.getKey().equals("Q")) {
                qlists=skillinfo;
            } else if (skillinfo.getRole_id().equals(id) && skillinfo.getKey().equals("W")) {
                wlists=skillinfo;
            } else if (skillinfo.getRole_id().equals(id) && skillinfo.getKey().equals("E")) {
                elists=skillinfo;
            } else if (skillinfo.getRole_id().equals(id) && skillinfo.getKey().equals("R")) {
                rlists=skillinfo;
            }
        }
        Picasso.with(getActivity()).load(plists.getPic_url()).into(skill_passive);
        Picasso.with(getActivity()).load(qlists.getPic_url()).into(skill_q);
        Picasso.with(getActivity()).load(wlists.getPic_url()).into(skill_w);
        Picasso.with(getActivity()).load(elists.getPic_url()).into(skill_e);
        Picasso.with(getActivity()).load(rlists.getPic_url()).into(skill_r);
        skill_name.setText(plists.getName());
        skill_introduce.setText(Html.fromHtml(plists.getDesc()));
        btn_seeVideo.setVisibility(View.GONE);
        if (plists.getCold()==null){
            skill_colling.setVisibility(View.GONE);
        }else{
            skill_colling.setText("冷却 : "+plists.getCold());
        }
        skill_expend.setVisibility(View.GONE);




    }

    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.skill_passive:
            btn_seeVideo.setVisibility(View.GONE);
            skill_expend.setVisibility(View.GONE);
            skill_name.setText(plists.getName());
            skill_introduce.setText(Html.fromHtml(plists.getDesc()));
            if (plists.getCold()==null){
                skill_colling.setVisibility(View.GONE);
            }else{
                skill_colling.setText("冷却 : "+plists.getCold());
            }
            rl_skill_passive.setBackgroundResource(R.drawable.hero_grid_bg_press);
            rl_skill_q.setBackgroundResource(0);
            rl_skill_w.setBackgroundResource(0);
            rl_skill_e.setBackgroundResource(0);
            rl_skill_r.setBackgroundResource(0);
            break;
        case R.id.skill_q:
            btn_seeVideo.setVisibility(View.VISIBLE);
            skill_expend.setVisibility(View.VISIBLE);
            skill_name.setText(qlists.getName());
            skill_introduce.setText(Html.fromHtml(qlists.getDesc()));
            if (qlists.getCold()==null){
                skill_colling.setVisibility(View.GONE);
            }else{
                skill_colling.setText("冷却 : "+qlists.getCold());
            }
            if (qlists.getMana()==null){
                skill_expend.setVisibility(View.GONE);
            }else{
                skill_expend.setText("耗蓝 : "+qlists.getMana());
            }
            btn_seeVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri =Uri.parse(qlists.getVideo_path());
                    Intent intent =new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(uri,"video/mp4");
                    startActivity(intent);
                }
            });
            rl_skill_q.setBackgroundResource(R.drawable.hero_grid_bg_press);
            rl_skill_passive.setBackgroundResource(0);
            rl_skill_w.setBackgroundResource(0);
            rl_skill_e.setBackgroundResource(0);
            rl_skill_r.setBackgroundResource(0);
        break;
        case R.id.skill_w:
            btn_seeVideo.setVisibility(View.VISIBLE);
            skill_expend.setVisibility(View.VISIBLE);
            skill_name.setText(wlists.getName());
            skill_introduce.setText(Html.fromHtml(wlists.getDesc()));
            if (wlists.getCold()==null){
                skill_colling.setVisibility(View.GONE);
            }else{
                skill_colling.setText("冷却 : "+wlists.getCold());
            }
            if (wlists.getMana()==null){
                skill_expend.setVisibility(View.GONE);
            }else{
                skill_expend.setText("耗蓝 : "+wlists.getMana());
            }
            btn_seeVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri =Uri.parse(wlists.getVideo_path());
                    Intent intent =new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(uri,"video/mp4");
                    startActivity(intent);
                }
            });
            rl_skill_w.setBackgroundResource(R.drawable.hero_grid_bg_press);
            rl_skill_q.setBackgroundResource(0);
            rl_skill_passive.setBackgroundResource(0);
            rl_skill_e.setBackgroundResource(0);
            rl_skill_r.setBackgroundResource(0);
        break;
        case R.id.skill_e:
            btn_seeVideo.setVisibility(View.VISIBLE);
            skill_expend.setVisibility(View.VISIBLE);
            skill_name.setText(elists.getName());
            skill_introduce.setText(Html.fromHtml(elists.getDesc()));
            if (elists.getCold()==null){
                skill_colling.setVisibility(View.GONE);
            }else{
                skill_colling.setText("冷却 : "+elists.getCold());
            }
            if (elists.getMana()==null){
                skill_expend.setVisibility(View.GONE);
            }else{
                skill_expend.setText("耗蓝 : "+elists.getMana());
            }
            btn_seeVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri =Uri.parse(elists.getVideo_path());
                    Intent intent =new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(uri,"video/mp4");
                    startActivity(intent);
                }
            });
            rl_skill_e.setBackgroundResource(R.drawable.hero_grid_bg_press);
            rl_skill_q.setBackgroundResource(0);
            rl_skill_w.setBackgroundResource(0);
            rl_skill_passive.setBackgroundResource(0);
            rl_skill_r.setBackgroundResource(0);
        break;
        case R.id.skill_r:
            btn_seeVideo.setVisibility(View.VISIBLE);
            skill_expend.setVisibility(View.VISIBLE);
            skill_name.setText(rlists.getName());
            skill_introduce.setText(Html.fromHtml(rlists.getDesc()));
            if (rlists.getCold()==null){
                skill_colling.setVisibility(View.GONE);
            }else{
                skill_colling.setText("冷却 : "+rlists.getCold());
            }
            if (rlists.getMana()==null){
                skill_expend.setVisibility(View.GONE);
            }else{
                skill_expend.setText("耗蓝 : "+rlists.getMana());
            }
            btn_seeVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri =Uri.parse(rlists.getVideo_path());
                    Intent intent =new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(uri,"video/mp4");
                    startActivity(intent);
                }
            });
            rl_skill_r.setBackgroundResource(R.drawable.hero_grid_bg_press);
            rl_skill_q.setBackgroundResource(0);
            rl_skill_w.setBackgroundResource(0);
            rl_skill_e.setBackgroundResource(0);
            rl_skill_passive.setBackgroundResource(0);
        break;
    }
    }
}



